﻿Imports System.Runtime.Serialization

<Serializable()>
Public Class LanguageLookup
    Implements ISerializable

#Region "Serialization"

    Private Sub New(info As SerializationInfo, context As StreamingContext)
        Me.New()

        Dim version As Integer = info.GetInt32("version")

        If version >= 1 Then
            mlanguages = info.GetValue(Of List(Of String))("languages")
            mTranslations = info.GetValue(Of Dictionary(Of String, List(Of String)))("translations")
            mWords = info.GetValue(Of Dictionary(Of String, RawObject))("words")
        End If

    End Sub


    Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
        info.AddValue("version", 1)

        'Version 1 data
        info.AddValue("languages", mlanguages)
        info.AddValue("translations", mTranslations)
        info.AddValue("words", mWords)
    End Sub
#End Region

    Private mlanguages As List(Of String)
    Private mTranslations As Dictionary(Of String, List(Of String)) 'key is the wordId, value is a list of translations in teh same order as mLanguages
    Private mWords As Dictionary(Of String, RawObject)

    Public Sub New()
        mlanguages = New List(Of String)
        mTranslations = New Dictionary(Of String, List(Of String))
        mWords = New Dictionary(Of String, RawObject)
    End Sub

    Public Sub AddLanguage(languageId As String)
        If Not mlanguages.Contains(languageId) Then
            mlanguages.Add(languageId)
            For Each translation As List(Of String) In mTranslations.Values
                translation.Add("")
            Next
        End If
    End Sub

    Public Sub AddWord(word As RawObject)
        If Not word.TypeCode.is(ObjectType.LANGUAGE, ObjectSubType.WORD) Then
            Throw New ArgumentException("object is not a word")
        End If
        If Not mWords.ContainsKey(word.objectId) Then
            mWords.Add(word.objectId, word)
            Dim translations As New List(Of String)
            mTranslations.Add(word.objectId, translations)
            For Each language As String In mlanguages
                translations.Add("")
            Next
        Else
            mWords(word.objectId) = word
        End If
    End Sub

    Default Public Property Translation(language As String, word As String) As String
        Get
            Dim languageIndex As Integer = mlanguages.IndexOf(language)
            If languageIndex = -1 Then
                Throw New ArgumentException("language not found")
            End If
            Return mTranslations(word)(languageIndex)
        End Get
        Set(value As String)
            Dim languageIndex As Integer = mlanguages.IndexOf(language)
            If languageIndex = -1 Then
                Throw New ArgumentException("language not found")
            End If
            If Not mTranslations.ContainsKey(word) Then 'temporarily add the word until the real full object can be read later
                Dim newWord As New RawObject(ObjectType.LANGUAGE, ObjectSubType.WORD)
                newWord.setObjectId(word)
                AddWord(newWord)
            End If
            mTranslations(word)(languageIndex) = value
        End Set
    End Property

    Public Sub Clear()
        mlanguages.Clear()
        mTranslations.Clear()
        mWords.Clear()
    End Sub

    Public ReadOnly Property Words() As IEnumerable(Of String)
        Get
            Return New List(Of String)(mWords.Keys)
        End Get
    End Property

    Public ReadOnly Property Languages As List(Of String)
        Get
            Return New List(Of String)(mlanguages)
        End Get
    End Property

    Public Function GetWordObject(wordId As String) As RawObject
        Return mWords(wordId)
    End Function

    'Public Function getDisconnectedClone() As LanguageLookup
    '    Dim ret As New LanguageLookup

    '    ret.mlanguages = New List(Of String)(mlanguages)

    '    ret.mWords = New Dictionary(Of String, RawObject)
    '    For Each obj As RawObject In mWords.Values
    '        Dim copy As New RawObject(obj.objectType, obj.objectSubType)
    '        copy.setObjectId(obj.objectId)
    '        copy.
    '    Next()
    'End Function
End Class
